﻿using System.Collections.Immutable;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace UserData.Data
{
    public interface IuserManager
    {
        Task <UserDto> CreateAsync(UserCreateDto user, CancellationToken cancellation = default);
        Task<List<UserDto>> GetAsync(int? id, CancellationToken cancellation = default);
        //Task<List<UserDto?>> GetAAsync(int id, CancellationToken cancellation = default);
    }
    internal class UserManager(DemoDbContext db) : IuserManager
    {
        public async Task<UserDto> CreateAsync(UserCreateDto dto, CancellationToken cancellation = default)
        {
            var user = new User
            {
                Name = dto.Name,
                Email = dto.Email,
                // Password = dto.Password,
                Tel = dto.Tel,
            };
            user.SetPassword(dto.Password);
            user.DateTime = DateTime.Now;
            db.Users.Add(user);
            await db.SaveChangesAsync(cancellation);
            return new UserDto
            {
                Id = user.Id,
                Name = dto.Name,
                Email = dto.Email,
                Password = dto.Password,
                Tel = dto.Tel,
                DateTime = user.DateTime,
            };
        }
        public async Task<List<UserDto?>> GetAsync(int? id, CancellationToken cancellation = default)
        {
            if (id == 0)
            {
                var userA = await db.Users.Select(x=>new UserDto { 
                Id = x.Id,
                Name = x.Name,
                Password = x.Password,
                Email = x.Email,
                Tel = x.Tel,
                DateTime = x.DateTime,
                }).ToListAsync();
                return userA;
            }
            //var user = await db.Users.Where(x => x.Id == id.Value)
            //    .Select(x => new UserDto { 
            //    Id = x.Id ,
            //    Name = x.Name ,
            //    Email = x.Email,
            //    Tel = x.Tel,
            //    DateTime = x.DateTime,
            //    }).FirstOrDefaultAsync(cancellation);
            //if (user == null) return null;
            //return new List<UserDto> { user };
            var user = await db.Users.FirstOrDefaultAsync(x => x.Id == id);
            if (user == null) return null;
            var users= new UserDto
            {
                Id = user.Id,
                Name = user.Name,
                Email = user.Email,
                Password = user.Password,
                Tel = user.Tel,
                DateTime = user.DateTime,
            };
            return new List<UserDto> { users };
        }
        //public async Task<List<UserDto?>> GetAAsync(int id, CancellationToken cancellation = default)
        //{

        //    var userA = await db.Users.Select(x => new UserDto
        //    {
        //        Id = x.Id,
        //        Name = x.Name,
        //        Email = x.Email,
        //        Tel = x.Tel,
        //        DateTime = x.DateTime,
        //    }).ToListAsync();
        //    return userA;
        //}
    }
    public class UserCreateDto
    {        
        public string Name { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Tel { get; set; }
        public DateTime DateTime { get; set; }
        
    }
    public class UserDto : UserCreateDto
    {
        public int Id { get; set; }
    }
}
